//跳转到明细页面
 var goEleDetailsPage;

var getMaintenaPlanEditDialog;
require(['grid', 'topBar', 'layout', 'date'], function () {
	var postData,rcx,type,orgIds;
	var $top = $('#top');
	var $parent = $top.parent();
	$('#top').topBar({
		left: [{
			type: 'filter-more',
			dateType: 'custom',
			search: function () {
				search();
			},
			item: [
				{
					text: '时间快捷',
					name1: 'beginTime',
					name2: 'endTime',
					type: 'dateLine'
				},
				{
					text: '差值(绝对值)>=',
					name: 'mile',
					width: '150',
					type: 'text'
				}
			]
		}
		],
		right: [
			{
				text: '导出',
				type: 'export-btn',
				authority: '',
				item: [
					{
						text: 'Excel导出',
						click: function () {
							exportFile('Excel');
						}
					},
					{
						text: 'PDF导出',
						click: function () {
							exportFile('Pdf');
						}
					}
				]
			}
		]
	});

	//获取左侧选择的节点信息
	function getPostData() {
		rcx = commonFun.getTreeCheckNodeToArgs();
		type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if (rcx) {
			postData = $.extend(rcx, $("#top").parent().getValues());
			postData.type = type;
			postData.orgIds = orgIds;
		}
		return rcx;
	}

	function exportFile(suffix) {
		if (getPostData()) {
			if ($('#grid').getGridParam("reccount") < 1) {
				$.messager.alert("提示", "请查询数据。", "info");
				return;
			}
			var url = "/mileContrast/exportMileContrast" + suffix + ".htm";
			$.exportFile(url, postData);
		}
		if ($('#grid').getGridParam("reccount") > 0) {
			if (getPostData()) {
				var url = "/mileContrast/exportMileContrast" + suffix + ".htm";
				$.exportFile(url, postData);
			}
		}
	}
	function gridInit() {
		$('#grid').grid({
			multiselect: false,
			groupHeader:true,
			url: "/mileContrast/searchMileContrast.htm",
			colModel: [
				{name: "carId", label: "carId",hidden:true},
				{name: "rcrdDate", label: "数据日期",width:85},
				{name: "lineName", label: "运营线路",width:70},
				{name: "licPltNo", label: "车牌号",width:70},
				{name: "carInCd", label: "车辆编号", width: 80},
				{name: "clientRngMile", label: "客户里程",width:75, align: "right"},
				{name: "clientMinRcrdTime", label: "最早数据时间",width:140},
				{name: "clientMaxRcrdTime", label: "最大数据时间",width:140},
				{name: "clientOper",label: '操作',width:100, title: false,formatter: 'operArea',formatoptions: [{
						text: '查看明细',
						css: 'operate-edit',
						click: function (data) {
							goEleDetailsPage(data.carId, data.rcrdDate,"1");
						}
					}]
				},
				{name: "cloudRngMile", label: "云总线里程",width:80, align: "right"},
				{name: "fixMile", label: "修正值",width:50, align: "right"},
				{name: "cloudMinRcrdTime", label: "最早数据时间",width:140},
				{name: "cloudMaxRcrdTime", label: "最晚数据时间",width:140},
				{name: "cloudOper",label: '操作',width:170, title: false,formatter: 'operArea', formatoptions: [{
						text: '能耗统计',
						css: 'operate-edit',
						click: function (data) {
							goEleDetailsPage(data.carId, data.rcrdDate,"2");
						}
					}, {
						text: '能耗明细',
						css: 'operate-edit',
						click: function (data) {
							goEleDetailsPage(data.carId, data.rcrdDate,"3");
						}
					}]

				},
				{name: "betweenMile", width:50,label: "差值", align: "right",formatter:function (cellvalue, options, rowObject){
					if(rowObject.betweenMile>20){
						return" <em style='color: red'>"+rowObject.betweenMile+"</em>";
					}else if(rowObject.betweenMile<-20){
						return" <em style='color: red'>"+rowObject.betweenMile+"</em>";
					}else{
						if(rowObject.betweenMile==null){
							return "";
						}else{
							return rowObject.betweenMile;
						}
					}
				}}
			]
		});

		$('#grid').grid('setGroupHeaders', {
			useColSpanStyle: true,
			groupHeaders: [{
				startColumnName: 'clientRngMile',
				numberOfColumns: 4,
				titleText: "<div class='t-header'>客户数据</div>"
			}, {
				startColumnName: 'cloudRngMile',
				numberOfColumns: 6,
				titleText: "<div class='t-header'>云平台数据</div>"
			}]
		});
	}

	function search() {
		var $mile = $(':text[name="mile"]',$parent);
		if(isNaN($mile.val())){
			$.messager.alert("提示", "请输入数字。", "info");
			return;
		}
		if (getPostData()) {
			$('#grid').grid('setGridParam', {
				page: 1,
				datatype: "json",
				postData: postData
			}).trigger("reloadGrid");
		}
	}

	//跳转到明细页面
	goEleDetailsPage = function (carId, rcrdDate,flag) {
		var data = {};
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		data.type=type;
		data.orgIds = carId;
		data.beginTime = rcrdDate;
		data.endTime = rcrdDate;
		data.carIds = carId;
		if(flag=="1"){
			//跳转到明细页面
			$.openTab('gfd2415883a94d72a90530e4ace3931d','showEleDetail',[data]);
		}else if(flag=="2"){
			//跳转到能耗表统计信息页面
			$.openTab('wer2415883a94d72a90530e4ace3931d','showEnergyStatistics',[data]);
		}else if(flag=="3"){
			//跳转到能耗明细页面
			data.carId = carId;
			$.openTab('qwe2415883a94d72a90530e4ace3931d','showEnergyDetail',[data]);
		}
	};


	$('body').layout();
	//初始化grid
	gridInit();
});